﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;

namespace PUBCOM
{
    public class MessageBox
    {
        private MessageBox()
        {
        }
        /// <summary>
        /// 执行javascript语句
        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="script">javascript语句</param>
        public static void ResponseScript(Page page, string script)
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(),"message", "<script language='javascript' defer>" + script + "</script>",true);
        }

        /// <summary>
        /// 跳出提示信息
        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="msg">提示信息</param>
        public static void Show(Page page, string msg)
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>", true);
        }
        /// <summary>
        /// 跳出提示信息　后返回


        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="msg">提示信息</param>
        public static void ShowAndBack(Page page, string msg)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.Append("history.back();");
            builder.Append("</script>");
            page.Response.Write(builder.ToString());
            page.Response.End();
        }
        /// <summary>
        /// 跳出提示信息并跳转ShowAndRedirect
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转地址</param>
        /// <param name="target">在哪个target跳转</param>
        public static void ShowAndRedirect(string msg, string url, string target)
        {
            HttpContext current = HttpContext.Current;
            StringBuilder builder = new StringBuilder();
            target = string.IsNullOrEmpty(target) ? "self" : target;
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("{1}.location='{0}'", url, target);
            builder.Append("</script>");
            current.Response.Write(builder.ToString());
            current.Response.End();
        }
        /// <summary>
        /// 跳出提示信息并跳转


        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转地址</param>
        public static void ShowAndRedirect(Page page, string msg, string url)
        {
            string target = "self";
            ShowAndRedirect(page, msg, url, target);
        }
        /// <summary>
        /// 跳出提示信息并跳转


        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转地址</param>
        /// <param name="target">在哪个target跳转</param>
        public static void ShowAndRedirect(Page page, string msg, string url, string target)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("{1}.location='{0}'", url, target);
            builder.Append("</script>");
            page.Response.Write(builder.ToString());
            page.Response.End();
        }
        /// <summary>
        /// 跳出提示信息并跳转 给控件添加confirm事件并返回ShowConfirm 
        /// </summary>
        /// <param name="Control"></param>
        /// <param name="msg"></param>
        public static void ShowConfirm(WebControl Control, string msg)
        {
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        /// <summary>
        /// 对不起！您无权操作


        /// </summary>
        /// <param name="msg"></param>
        public static void ShowErrorMsg(string msg)
        {
            msg = string.IsNullOrEmpty(msg) ? "对不起！您无权操作。" : msg;
            HttpContext current = HttpContext.Current;
            current.Response.Write("<script language=javascript>alert('" + msg + "');history.back();</script>");
            current.Response.End();
        }

        /// <summary>
        /// 弹出框
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="msg">显示信息</param>
        /// <param name="type">类型 1页面转向 2刷新父窗体 3刷新父窗体，关闭子窗体 4返回上一页 5关闭本窗口打开子窗口 7 不传URL 刷新父窗体关闭子窗体</param>
        public static void ShowMsg(string url, string msg, int type)
        {
            HttpContext current = HttpContext.Current;
            current.Response.Write("<Script Language=\"javascript\">\n");
            if (msg.Trim() != "")
                current.Response.Write("alert(\"" + msg + "\")\n");
            switch (type)
            {
                //页面转向
                case 1:
                    current.Response.Write("location.href=\"" + url + "\"\n");
                    break;
                //刷新父窗体


                case 2:
                    current.Response.Write("window.opener.location.href=\"" + url + "\"\n");
                    break;
                //刷新父窗体，关闭子窗体


                case 3:
                    current.Response.Write("window.opener.location.href=\"" + url + "\"\n");
                    current.Response.Write("window.close();\n");
                    break;
                //返回上一页


                case 4:
                    current.Response.Write("history.back();\n");
                    break;
                //关闭本窗口打开子窗口


                case 5:
                    current.Response.Write("window.close();\n");
                    current.Response.Write("window.opener.window.open(\"" + url + "\")");
                    break;
                case 6:
                    current.Response.Write("window.parent.location.reload();\n");
                    break;
                case 7:
                    current.Response.Write("window.opener.location.href=window.opener.location.href;\n");
                    current.Response.Write("window.close();\n");
                    break;
                default :
                    
                    break;
            }
            current.Response.Write("</Script>");
        }

        /// <summary>
        /// 弹出提示对话框
        /// 动作 0：无动作，1：转向一个新页面，2：关闭子窗口，3：刷新父窗口，4：刷新父窗口并关闭子窗口，5：返回上一页，6：关闭本窗口，打开子窗口,7:关闭子窗口，刷新父窗口（用于ymPrompt中的弹出窗口）,关闭子窗口（用于ymPrompt中的弹出窗口）
        /// </summary>
        /// <param name="str_url">页面地址</param>
        /// <param name="str_message">提示信息</param>
        /// <param name="I_op">动作 0：无动作，1：转向一个新页面，2：关闭子窗口，3：刷新父窗口，4：刷新父窗口并关闭子窗口，5：返回上一页，6：关闭本窗口，打开子窗口,7:关闭子窗口，刷新父窗口（用于ymPrompt中的弹出窗口）</param>
        public static void PoP_Notice_Msg(string str_url, string str_message, int I_op)
        {
            HttpContext.Current.Response.Write("<Script Language=\"javascript\">\n");
            if (str_message != "")
                HttpContext.Current.Response.Write("alert(\"" + str_message + "\")\n");
            switch (I_op)
            {
                case 1:
                    HttpContext.Current.Response.Write("location.href=\"" + str_url + "\"\n");
                    break;
                case 2:
                    HttpContext.Current.Response.Write("window.close();\n");
                    break;
                case 3:
                    HttpContext.Current.Response.Write("window.opener.location.href=\"" + str_url + "\"\n");
                    break;
                case 4:
                    HttpContext.Current.Response.Write("window.opener.location.href=\"" + str_url + "\"\n");
                    HttpContext.Current.Response.Write("window.close();\n");
                    break;
                case 5:
                    HttpContext.Current.Response.Write("window.history.back(-1);\n");
                    break;
                case 6:
                    HttpContext.Current.Response.Write("window.close();\n");
                    HttpContext.Current.Response.Write("window.opener.window.open(\"" + str_url + "\")");
                    break;
                case 7:
                    HttpContext.Current.Response.Write("window.parent.location.reload();\n");

                    break;
                case 8:
                    HttpContext.Current.Response.Write("javascript:window.parent.ymPrompt.close();");
                    break;

                case 9:
                    HttpContext.Current.Response.Write("window.opener.location.href=window.opener.location.href;\n");
                    HttpContext.Current.Response.Write("window.close();\n");
                    break;
                case 10:
                    HttpContext.Current.Response.Write("javascript:window.parent.ymPrompt.close();");
                    break;
                case 11:
                    HttpContext.Current.Response.Write("window.parent.parent.location.href=\"" + str_url + "\"\n");
                    HttpContext.Current.Response.Write("window.close();\n");
                    break;
                case 12:
                    HttpContext.Current.Response.Write("window.close();\n");
                    HttpContext.Current.Response.Write("window.parent.location.reload();\n");

                    break;
                case 13:
                    HttpContext.Current.Response.Write("window.parent.location.href=\"" + str_url + "\"\n");
                    HttpContext.Current.Response.Write("window.close();\n");
                    break;

                    // 关闭子窗体，刷新父窗体

                case 14:
                    HttpContext.Current.Response.Write("window.close();\n");
                    HttpContext.Current.Response.Write("window.opener.location.reload();window.opener=null \n");
 
					break;
				case 15:
					HttpContext.Current.Response.Write("window.parent.location.href=window.parent.location.href;\n");
					HttpContext.Current.Response.Write("window.close();\n");
					break;
                case 16:
                    HttpContext.Current.Response.Write("window.parent.location.href=window.parent.location.href;\n");
                    break;
                case 21:
                    HttpContext.Current.Response.Write("window.parent.location.href=\"" + str_url + "\"\n");
                    break;
                
               
            }
            HttpContext.Current.Response.Write("</Script>");
        }

        /// <summary>
        /// 弹出提示对话框(带由AJAX控件)
        /// 动作 0：无动作，1：转向一个新页面，2：关闭子窗口，3：刷新父窗口，4：刷新父窗口并关闭子窗口，5：返回上一页，6：关闭本窗口，打开子窗口
        /// </summary>
        /// <param name="page">当前页面:可直接写this.Page</param>
        /// <param name="str_url">页面地址</param>
        /// <param name="str_message">提示信息</param>
        /// <param name="I_op">动作 0：无动作，1：转向一个新页面，2：关闭子窗口，3：刷新父窗口，4：刷新父窗口并关闭子窗口，5：返回上一页，6：关闭本窗口，打开子窗口,7:关闭子窗口，刷新父窗口（用于ymPrompt中的弹出窗口）</param>
        public static void PoP_Notice_Msg(Page page, string str_url, string str_message, int I_op)
        {
            StringBuilder str = new StringBuilder();
            if (str_message != "")
                str.Append("alert(\"" + str_message + "\")\n");
            switch (I_op)
            {
                case 1:
                    str.Append("location.href=\"" + str_url + "\"\n");
                    break;
                case 2:
                    str.Append("window.close();\n");
                    break;
                case 3:
                    str.Append("window.opener.location.href=\"" + str_url + "\"\n");
                    break;
                case 4:
                    str.Append("window.opener.location.href=\"" + str_url + "\"\n");
                    str.Append("window.close();\n");
                    break;
                case 5:
                    str.Append("history.back();\n");
                    break;
                case 6:
                    str.Append("window.close();\n");
                    str.Append("window.opener.window.open(\"" + str_url + "\")");
                    break;
                case 7:
                    str.Append("closeDialog();\n");
                    break;
                case 8:
                    str.Append("window.parent.location.reload();\n");
                    break;
				case 9:
					str.Append("window.parent.ymPrompt.close();\n");
					str.Append("window.parent.location.reload();\n");
					break;
                case 10:
                    str.Append("window.close();\n");
                    str.Append("window.opener.location.reload();\n");
                    break;
                case 21:
                    str.Append("window.parent.location.href=\"" + str_url + "\"\n");
                    break;
            }
           
            ScriptManager.RegisterStartupScript(page, page.GetType(), "check", str.ToString(), true);
        }

        #region ajax跳出窗口

        /// <summary>
        /// 执行javascript语句Control Control
        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="script">javascript语句</param>
        public static void ResponseScriptAjax(Page page, string script)
        {
            //page.RegisterStartupScript("message", "<script language='javascript' defer>" + script + "</script>");
            ScriptManager.RegisterStartupScript(page, page.GetType(), "onclick", script, true);
        }
        /// <summary>
        /// 执行javascript语句Control Control
        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="script">javascript语句</param>
        public static void ResponseScriptAjax(Control control, string script)
        {
            //page.RegisterStartupScript("message", "<script language='javascript' defer>" + script + "</script>");
            ScriptManager.RegisterStartupScript(control, control.GetType(), "onclick", script, true);
        }


        /// <summary>
        /// 跳出提示信息
        /// </summary>
        /// <param name="page">当前Page</param>
        /// <param name="msg">提示信息</param>
        public static void ShowAjax(Page page, string msg)
        {
            ScriptManager.RegisterStartupScript(page, page.GetType(), "onclick", "ymPrompt.alert({message:'<div style=\"text-align:left; width:100%\">" + msg.ToString() + "</div>'});", true);
        }

        /// <summary>
        /// 跳出提示信息并跳转ShowAndRedirect
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转地址</param>
        /// <param name="target">在哪个target跳转</param>
        public static void ShowAndRedirectAjax(Page page, string msg, string url, string target)
        {
            HttpContext current = HttpContext.Current;
            StringBuilder builder = new StringBuilder();
            target = string.IsNullOrEmpty(target) ? "self" : target;
            // "window.ymPrompt.alert({message:'该邮箱已经被注册了'}),handler:function(){alert('good')}}
            string str = "ymPrompt.alert({message:'<div style=\"text-align:left; width:100%\">" + msg + "</div>',handler:function(){" + target + ".location.href='" + url + "'}});";
            //builder.AppendFormat("{1}.location='{0}'", url, target);
            ScriptManager.RegisterStartupScript(page, page.GetType(), "onclick", str, true);
            //current.Response.End();
        }

        /// <summary>
        /// 跳出提示信息并跳转ShowAndRedirect
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转地址</param>
        /// <param name="target">在哪个target跳转</param>
        public static void ShowAndRedirectAjax(Page page, string msg, string url)
        {
            ShowAndRedirectAjax(page, msg, url, null);
        }

        #endregion

        #region  弹出新窗口 WinOpen
        /// <summary>
        /// 弹出新窗口
        /// </summary>
        /// <param name="url">路径</param>
        /// <param name="top">离页面顶部距离</param>
        /// <param name="left">离页面左边距离</param>
        /// <param name="width">弹出页面宽度</param>
        /// <param name="height">弹出页面高度</param>
        /// <param name="scrollbars">是否滚动条 yes:有  no:无</param>
        /// <param name="resizable">是否能调节大小 yes:能  no:不能</param>
        public static void WinOpen(string url, int top, int left, int width, int height, string scrollbars, string resizable)
        {
            string strurl = "window.open('{0}','','top={1},left={2},width={3},height={4},scrollbars={5},resizable={6}')";
            strurl = string.Format(strurl, url, top, left, width, height, scrollbars, resizable);
            HttpContext.Current.Response.Write("<Script Language=\"javascript\">\n");
            HttpContext.Current.Response.Write(strurl + "\n");
            HttpContext.Current.Response.Write("</Script>");
        }

        public static void WinOpen(Page page, string url, int top, int left, int width, int height, string scrollbars, string resizable)
        {
            string strurl = "window.open('{0}','','top={1},left={2},width={3},height={4},scrollbars={5},resizable={6}')";
            strurl = string.Format(strurl, url, top, left, width, height, scrollbars, resizable);
            //HttpContext.Current.Response.Write("<Script Language=\"javascript\">\n");
            //HttpContext.Current.Response.Write(strurl + "\n");
            //HttpContext.Current.Response.Write("</Script>");
            ScriptManager.RegisterStartupScript(page, page.GetType(), "check", strurl.ToString(), true);
        }

        public static string WinOpenStr(string url, int top, int left, int width, int height, string scrollbars, string resizable)
        {
            //string strwin = string.Empty;
            string strurl = "window.open(\"{0}\",\"\",\"top={1},left={2},width={3},height={4},scrollbars={5},resizable={6}\")";
            strurl = string.Format(strurl, url, top, left, width, height, scrollbars, resizable);
            //strwin = "<Script Language=\"javascript\">" + strurl + "</Script>";
            return strurl;
        }
        #endregion
    }
}
